package com.pro.controller;

import com.pro.pojo.Product;
import com.pro.pojo.SyCourseRecord;
import com.pro.service.ProductService;
import com.pro.utils.ExcelUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author MYH
 * @date 2021/11/11 9:28
    导入Excel
 */

@RequestMapping("/Excel")
@Controller
public class ExcelController {

    @Autowired
    private ProductService ps;

    @RequestMapping("/importExcel")
    @ResponseBody
    public void importExcel(MultipartFile fil) throws IOException, ParseException {
        String end = fil.getOriginalFilename().substring(fil.getOriginalFilename().indexOf("."));
        List<Product> pro = null;
        if(end.equals(".xls")){
            pro = xlsExcel(fil);
        }else{
            pro = xlsxExcel(fil);
        }

        Boolean aBoolean = null;
        for (Product pros : pro) {
            aBoolean = ps.addProduct(pros);
        }
        System.err.println(aBoolean);
    }


    private List<Product> xlsExcel(MultipartFile fil) throws IOException, ParseException {
        ArrayList<Product> list = new ArrayList<>();
        InputStream inputStream = fil.getInputStream();
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet= workbook.getSheetAt(0);

        for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
            HSSFRow row = sheet.getRow(rowNum);
            Product pro = null;
            SyCourseRecord sy =null;
            if(row != null){
                pro = new Product();
                sy=new SyCourseRecord();
                sy.setCourseId(ExcelUtils.StrisInt(row.getCell(0).toString()));
                sy.setApplyDate(new Date());
                sy.setLaunchId(row.getCell(14).toString());
                sy.setImplementId(row.getCell(16).toString());
                sy.setImplementDate(new Date());
                sy.setResult(2);
                sy.setType(3);
                ps.addCourseRecord(sy);
                pro.setProductId(ExcelUtils.StrisInt(row.getCell(0).toString()));
                pro.setProductCode(ExcelUtils.StrisInt(row.getCell(1).toString()));
                pro.setCategroyId(ExcelUtils.StrisInt(row.getCell(2).toString()));
                pro.setRecommend(ExcelUtils.StrisInt(row.getCell(3).toString()));
                pro.setDiscount(ExcelUtils.StrisInt(row.getCell(4).toString()));
                pro.setEnsure(ExcelUtils.StrisInt(row.getCell(5).toString()));
                pro.setProductName((row.getCell(6).toString()));
                pro.setSearchKeywords((row.getCell(7).toString()));
                pro.setMainImage((row.getCell(8).toString()));
                pro.setCurrentpriceProduct(ExcelUtils.StrisInt(row.getCell(9).toString()));
                pro.setOriginalpriceProduct(ExcelUtils.StrisInt(row.getCell(10).toString()));
                pro.setProductDescription((row.getCell(11).toString()));
                pro.setHits(ExcelUtils.StrisInt(row.getCell(12).toString()));
                pro.setProductSate(ExcelUtils.StrisInt(row.getCell(13).toString()));
                pro.setCreateId(row.getCell(14).toString());
                pro.setCreateDate(row.getCell(15).toString());
                pro.setUpdateId(row.getCell(16).toString());
                pro.setUpdateDate(row.getCell(17).toString());
                pro.setProductLabel(row.getCell(18).toString());
            }
            list.add(pro);
        }
        return list;
    }

    private List<Product> xlsxExcel(MultipartFile fil) throws IOException, ParseException {
        ArrayList<Product> list = new ArrayList<>();
        InputStream inputStream = fil.getInputStream();
        XSSFWorkbook Workbook = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = Workbook.getSheetAt(0);

        for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
            XSSFRow row = sheet.getRow(rowNum);
            Product pro = null;
            SyCourseRecord sy =null;
            if(row != null){
                pro = new Product();
                sy=new SyCourseRecord();
                sy.setCourseId(ExcelUtils.StrisInt(row.getCell(0).toString()));
                sy.setApplyDate(new Date());
                sy.setLaunchId(row.getCell(14).toString());
                sy.setImplementId(row.getCell(16).toString());
                sy.setImplementDate(new Date());
                sy.setResult(2);
                sy.setType(3);
                ps.addCourseRecord(sy);
                pro.setProductId(ExcelUtils.StrisInt(row.getCell(0).toString()));
                pro.setProductCode(ExcelUtils.StrisInt(row.getCell(1).toString()));
                pro.setCategroyId(ExcelUtils.StrisInt(row.getCell(2).toString()));
                pro.setRecommend(ExcelUtils.StrisInt(row.getCell(3).toString()));
                pro.setDiscount(ExcelUtils.StrisInt(row.getCell(4).toString()));
                pro.setEnsure(ExcelUtils.StrisInt(row.getCell(5).toString()));
                pro.setProductName((row.getCell(6).toString()));
                pro.setSearchKeywords((row.getCell(7).toString()));
                pro.setMainImage((row.getCell(8).toString()));
                pro.setCurrentpriceProduct(ExcelUtils.StrisInt(row.getCell(9).toString()));
                pro.setOriginalpriceProduct(ExcelUtils.StrisInt(row.getCell(10).toString()));
                pro.setProductDescription((row.getCell(11).toString()));
                pro.setHits(ExcelUtils.StrisInt(row.getCell(12).toString()));
                pro.setProductSate(ExcelUtils.StrisInt(row.getCell(13).toString()));
                pro.setCreateId(row.getCell(14).toString());
                pro.setCreateDate(row.getCell(15).toString());
                pro.setUpdateId(row.getCell(16).toString());
                pro.setUpdateDate(row.getCell(17).toString());
                pro.setProductLabel(row.getCell(18).toString());
            }
            list.add(pro);
        }
        return list;
    }

}
